<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="口岸名称" prop="port_name">
        <el-input
          v-model="queryParams.port_name"
          placeholder="口岸名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="口岸代码" prop="port_code">
        <el-input
          v-model="queryParams.port_code"
          placeholder="口岸代码"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-action="'myPort@createPort'"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleCreate({},'添加')"
        >添加
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" style="width: 100%;">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="企业名称">
              {{ scope.row.default_ebc_name }}
            </el-form-item>
            <el-form-item label="账册企业名称">
              {{ scope.row.ems_name }}
            </el-form-item>
            <el-form-item label="企业代码">
              {{ scope.row.default_ebc_code }}
            </el-form-item>
            <el-form-item label="账册企业代码">
              {{ scope.row.ems_code }}
            </el-form-item>
            <el-form-item label="DXPID">
              {{ scope.row.dxp_id }}
            </el-form-item>
            <el-form-item label="企业账册编号">
              {{ scope.row.ems_no }}
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="口岸名称" align="center" prop="port_name" />
      <el-table-column label="口岸代码" align="center" prop="port_code" />
      <el-table-column label="申报类型" align="center" prop="service_type">
        <template v-slot="scope">
          <span v-if="scope.row.service_type === 1" style="color: #1890ff;">服务商</span>
          <span v-if="scope.row.service_type === 2" style="color: #1890ff;">商户自营</span>
        </template>
      </el-table-column>
      <el-table-column label="进口状态" align="center" prop="import_status">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.import_status"
            active-text="启用"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="2"
            :disabled="true"
          />
        </template>
      </el-table-column>
      <el-table-column label="出口状态" align="center" prop="export_status">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.export_status"
            active-text="启用"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="2"
            :disabled="true"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="created_at" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template v-slot="scope">
          <el-button
            v-action="'myPort@updatePort'"
            size="mini"
            type="primary"
            :disabled="scope.row.id === 1"
            @click="handleCreate(scope.row,'编辑')"
          >编辑</el-button>
          <el-button
            v-action="'myPort@delPort'"
            size="mini"
            type="danger"
            :disabled="scope.row.id === 1"
            @click="delPort(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 新增 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" class="demo-ruleForm">

        <el-form-item label="申报口岸" prop="port_id">
          <el-select v-model="form.port_id" placeholder="请选择口岸" style="width:100%">
            <el-option
              v-for="(dict,index) in portConfig"
              :key="index"
              :label="dict.port_name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="服务模式" prop="service_type">
          <span slot="label">
            申报模式
            <el-tooltip content="服务商：传输报文及账册企业启用服务商信息；商户自营：需要商户自行申请并配置传输报文企业及账册企业信息" placement="top">
              <i class="el-icon-info" />
            </el-tooltip>
          </span>
          <el-radio-group v-model="form.service_type">
            <el-radio :label="1">服务商</el-radio>
            <el-radio :label="2">商户自营</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="进口服务" prop="import_status">
          <el-switch
            v-model="form.import_status"
            active-text="启用"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="2"
          />
        </el-form-item>

        <el-form-item label="出口服务" prop="export_status">
          <el-switch
            v-model="form.export_status"
            active-text="启用"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="2"
          />
        </el-form-item>

        <el-form-item prop="m_ebc_name">
          <span slot="label">
            企业名称
            <el-tooltip content="报文传输的企业名称（一般为海关备案的电商企业）" placement="top">
              <i class="el-icon-info" />
            </el-tooltip>
          </span>
          <el-input v-model="form.m_ebc_name" />
        </el-form-item>

        <el-form-item prop="m_ebc_code">
          <span slot="label">
            企业代码
            <el-tooltip content="报文传输的企业代码（一般为海关备案的电商企业）" placement="top">
              <i class="el-icon-info" />
            </el-tooltip>
          </span>
          <el-input v-model="form.m_ebc_code" />
        </el-form-item>
        <el-form-item prop="m_dxp_id">
          <span slot="label">
            DXPID
            <el-tooltip content="中国电子口岸数据中心申请数据交换平台的用户编号" placement="top">
              <i class="el-icon-info" />
            </el-tooltip>
          </span>
          <el-input v-model="form.m_dxp_id" />
        </el-form-item>
        <el-form-item prop="m_ems_name">
          <span slot="label">
            账册企业名称
            <el-tooltip content="账册商品备案企业主体的名称" placement="top">
              <i class="el-icon-info" />
            </el-tooltip>

          </span>
          <el-input v-model="form.m_ems_name" />
        </el-form-item>
        <el-form-item prop="m_ems_code">
          <span slot="label">
            账册企业代码
            <el-tooltip content="账册商品备案企业主体的海关代码" placement="top">
              <i class="el-icon-info" />
            </el-tooltip>

          </span>
          <el-input v-model="form.m_ems_code" />
        </el-form-item>

        <el-form-item label="" prop="m_ems_no">
          <span slot="label">
            企业账册编号
            <el-tooltip content="账册商品备案企业主体的账册编号" placement="top">
              <i class="el-icon-info" />
            </el-tooltip>

          </span>
          <el-input v-model="form.m_ems_no" />
        </el-form-item>

        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="open=false">取消</el-button>

          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>

</template>
<style>
.el-input__inner {
  width: 90%;
}
.el-input__suffix {
  right: 10%;
}
</style>
<script>
import { myPortList, createMyPort, updateMyPort, delMyPort, portConfig } from '@/api/port'
export default {
  name: 'Index',
  data() {
    var checkMerchant = (rule, value, callback) => {
      if (this.form.service_type === 2 && !value) {
        callback(new Error('商户自营模式，不能为空'))
      } else {
        callback()
      }
    }
    return {
      // 遮罩层
      loading: true,
      open: false,
      title: '',
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      portConfig: [],
      info: '',
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        port_name: undefined,
        port_code: undefined
      },
      form: {},
      show: false,
      ids: [],
      rules: {
        port_id: [
          { required: true, message: '请选择口岸', trigger: 'change' }
        ],
        service_type: [
          { required: true, message: '请选择申报模式', trigger: 'change' }
        ],
        import_status: [
          { required: true, message: '请选择进口服务', trigger: 'change' }
        ],
        export_status: [
          { required: true, message: '请选择出口服务', trigger: 'change' }
        ],
        m_ebc_name: [
          { validator: checkMerchant, trigger: 'blur' }
        ],
        m_ebc_code: [
          { validator: checkMerchant, trigger: 'blur' }
        ],
        m_dxp_id: [
          { validator: checkMerchant, trigger: 'blur' }
        ],
        m_ems_name: [
          { validator: checkMerchant, trigger: 'blur' }
        ],
        m_ems_code: [
          { validator: checkMerchant, trigger: 'blur' }
        ],
        m_ems_no: [
          { validator: checkMerchant, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    /** 详情 */
    detail(row) {
      // orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
      //   if (response.code === 10000) {
      //     this.form = response.data
      //     this.show = true
      //   }
      // })
    },
    /** 打开 */
    handleCreate(row, title) {
      const that = this
      portConfig({}).then(response => {
        that.portConfig = response.data
        if (that.$refs['form']) {
          that.$refs['form'].clearValidate()
        }
        that.form = row || {}
        that.title = title
        that.open = true
      })
    },
    /** 查询清单列表 */
    getList() {
      this.loading = true
      // console.log(this.queryParams)
      myPortList(this.queryParams).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.count
        this.loading = false
      })
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!that.form.id) {
            createMyPort(that.form).then(response => {
              // console.log(response)
              that.$message.success(response.message)
              that.open = false
              that.getList()
            })
          } else {
            updateMyPort(that.form).then(response => {
              // console.log(response)
              that.$message.success(response.message)
              that.open = false
              that.getList()
            })
          }
        } else {
          return false
        }
      })
    },
    delPort(row) {
      const that = this
      that.$confirm('是否删除口岸' + row.port_name, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        delMyPort({ id: row.id }).then(response => {
          that.$message.success(response.message)
          that.getList()
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        port_name: undefined,
        port_code: undefined
      }
      this.handleQuery()
    },
    /** 选中的项 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.multiple = !selection.length
    }
  }
}
</script>

